package es.usj.apps.moviles.parsers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.util.Log;

import com.google.android.maps.GeoPoint;

public class RouteParser {

	public GeoPoint origin = null;
	public GeoPoint destination = null;
	
	public interface OnDrawRouteListener {
		public void onDrawRoute(String result, GeoPoint origin, GeoPoint destination);
	}

	OnDrawRouteListener mListenerRoute = null;

	public void setOnDrawRouteListener(OnDrawRouteListener listener) {
		mListenerRoute = listener;
	}
	
	public RouteParser(GeoPoint origin, GeoPoint destination)
	{
		this.origin = origin;
		this.destination = destination;
	}
	
	private static String convertStreamToString(InputStream is) {
		/*
		 * To convert the InputStream to String we use the
		 * BufferedReader.readLine() method. We iterate until the BufferedReader
		 * return null which means there's no more data to read. Each line will
		 * appended to a StringBuilder and returned as String.
		 */
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

	public String connectRoute(String url)
	{
		HttpClient httpclient = new DefaultHttpClient();

		HttpGet httpget = new HttpGet(url);

		HttpResponse response;
		
		String result = "";
		
		try {
			
			response = httpclient.execute(httpget);

			Log.i("Electric-Vehicle", response.getStatusLine().toString());

			HttpEntity entity = response.getEntity();

			if (entity != null) {

				// A Simple JSON Response Read
				InputStream instream = entity.getContent();
				result = convertStreamToString(instream);
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public void startConnection(String url) {

		Log.i("Electric-vehicle", url);
		new ConnectionAsync().execute(url);
	}

	private class ConnectionAsync extends AsyncTask<String, Void, String> {

		@Override
		protected void onPreExecute() {

			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... arg0) {

			String result = null;

			if (arg0.length > 0) {
				String url = arg0[0];
				result = connectRoute(url);
			}

			return result;
		}

		@Override
		protected void onPostExecute(String result) {
			if (mListenerRoute != null) {
				mListenerRoute.onDrawRoute(result, origin, destination);
			}
		}
	}
}
